package com.atguigu.mapreduce.hbase;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;

import java.io.IOException;

/**
 * @author laberinto
 * @version 1.0
 * @date 2024/10/8 13:37
 * @package com.atguigu.mapreduce.hbase
 * @message \\TODO
 * (⌓‿⌓)
 */
public class hbaseCon {



    public static void createTable(String tableName, String... columnFamily) {
        Configuration conf = HBaseConfiguration.create();
        Admin admin = null;
        conf.set("hbase.rootdir", "hdfs://master:9000/hbase");
        conf.set("hbase.zookeeper.quorum", "master,slave1,slave2");

        try {
            Connection connection = ConnectionFactory.createConnection(conf);
            admin = connection.getAdmin();
        } catch (IOException e) {
            e.printStackTrace();
        }
        TableName tableNameObj = TableName.valueOf(tableName);
        try {
            if (admin.tableExists(tableNameObj)) {
                System.out.println("Table: " + tableName + " already exists!");
            } else {
                HTableDescriptor tb = new HTableDescriptor(tableNameObj);
                for (int i = 0; i < columnFamily.length; i++) {
                    HColumnDescriptor family = new HColumnDescriptor(columnFamily[i]);
                    tb.addFamily(family);
                }
                admin.createTable(tb);
                System.out.println(tableName + "创建成功");
            }
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println(tableName + "创建失败");
        }
    }

    public static void main(String[] args) {
        createTable("student", "cf1","cf2");

    }


}
